﻿using System;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.Org
{
    public partial class EditASupportingMaterials : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    string id = Request.QueryString["id"];
                    LoadAllData(int.Parse(id));
                }
            }
        }

        private void LoadData()
        {
            DropDownListType.DataSource = ASupportingMaterialBo.LoadAllType().ToList();
            DropDownListType.DataTextField = "Type";
            DropDownListType.DataBind();
        }

        private void LoadAllData(int id)
        {
            SupportingMaterial data = ASupportingMaterialBo.GetSupportingMaterialById(id);
            data.SupportingMaterialId = id;
            TextBoxURL.Text = data.Url;
            TextBoxAddedBy.Text = data.User.Account;
            TextBoxAddedDate.Text = String.Format("{0:dd/MM/yyyy}", data.AddedDate);
            TextBoxDescription.Text = data.Description;
            DropDownListType.Text = data.Type;
        }

        protected void ButtonSave_Command(object sender, CommandEventArgs e)
        {
            var supportingMaterial = new SupportingMaterial();
            //var organisation=from DB.Con
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                supportingMaterial.SupportingMaterialId = int.Parse(Request.QueryString["id"],
                                                                    CultureInfo.CurrentCulture);
            }
            supportingMaterial.Url = TextBoxURL.Text;
            //supportingMaterial.User.Account = HttpContext.Current.User.Identity.Name;
            supportingMaterial.Description = TextBoxDescription.Text;
            supportingMaterial.Type =DropDownListType.Text;
            //gan cung sua sau
            supportingMaterial.OrganisationId = 3;
            supportingMaterial.UserId = 2;

            bool updateSupportingMaterial = ASupportingMaterialBo.UpdateSupportingMaterial(supportingMaterial);
            if (updateSupportingMaterial)
            {
                Response.Redirect("~/Organisation/ASupportingMaterials.aspx");
            }
            else
            {
                Response.Write("<script>alert('Update To Database Failed!')</script>");
            }
        }
    }
}